package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import model.mongo.RecommendUser;
import model.mongo.UserLike;
import model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
@Slf4j
public class RecommendUserApiImpl implements RecommendUserApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    //分页查询
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //构建Query对象
        log.info("page:"+page+",size:"+pagesize+",toUserId:"+toUserId);
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        //总记录数
        Long count = mongoTemplate.count(query, RecommendUser.class);
        //配置分页参数
        query.skip((page-1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("score")));
        //查询数据列表
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        log.info("recommendUsers:"+recommendUsers);
        return new PageResult(page, pagesize, count, recommendUsers);
    }

    @Override
    public RecommendUser queryByUserId(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        if(user == null) {
            user = new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(toUserId);
            //构建缘分值
            user.setScore(95d);
        }
        return user;
    }

    @Override
    public List<RecommendUser> queryCardsList(Long userId, int count) {
        List<UserLike> userLikeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikeList, "likeUserId", Long.class);

        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);

        TypedAggregation<RecommendUser> recommendUserTypedAggregation
                = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(count));

        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(recommendUserTypedAggregation, RecommendUser.class);
        return results.getMappedResults();
    }
}
